realOf error "incorrect argument"

When using the code below we get the following error "-E- DXL: <Line:5> Incorrect argument for function (realOf)".  Can anyone tell me what is wrong with line 5?

real calcCost(Object o){

  real rAccCost = 0.0

  Object objChild = null

  for objChild in o do {

      rAccCost += realOf(objChild."Cost") 

      rAccCost += calcCost(objChild)

  }

  return rAccCost

}

real r = calcCost(obj)

if(r > 0.0) obj.attrDXLName = r


Tap5 - Wed Apr 09 15:21:10 EDT 2014

Re: realOf error "incorrect argument"
SudarshanRao - Thu Apr 10 03:38:09 EDT 2014

Suffix the attribute with a blank string, that I think should do the trick. So your fifth line should be:

rAccCost += realOf(objChild."Cost" "")

Re: realOf error "incorrect argument"
llandale - Thu Apr 10 15:07:09 EDT 2014

SudarshanRao - Thu Apr 10 03:38:09 EDT 2014

Suffix the attribute with a blank string, that I think should do the trick. So your fifth line should be:

rAccCost += realOf(objChild."Cost" "")

Yes.

Discussion:

  • obj.NameAttr returns a variable of type "Attr__" (which we don't use)
  • realOf(Variable) requires Variable to be type "string" or "int"; but in your case its getting a variable of type "Attr__" and thus the error.
  • string s = obj.NameAttr works because there is a DXL "perm" of an equal sign which gets the value of that attribute and sets it to the string.
    • string = Attr__
  • Attr__ ""  uses the concatenate perm, which retrieves the value from the attribute and converts it to a string.
  • string s = obj.NameAttr "" uses a different equal sign perm; first retrieving the value, converting to a string, THEN assigning it to s.
  • Another time you must concatenate an Attr__ with a string is in "if" statements:
    • if (obj.NameAttr "" == "Whatever") then..

It is indeed confusing.  In general, routinely use double quotes after o.NameAttr to make it work; except during assignment statments.

-Louie